import requests, asyncio
import json

from config import HEARTBEAT_URL, PHONE

async def heartbeat_loop(client, interval=30):
    print(f"[DEBUG] 进入函数: heartbeat_loop")
    
    # 获取客户端手机号
    phone_number = PHONE
    
    while client.is_connected():
        try:
            # 模拟网络请求上传心跳
            heartbeat_data = {
                'phone': phone_number,
                'status': 'online'
            }
            print(f"[DEBUG] 模拟发送心跳数据: {json.dumps(heartbeat_data, ensure_ascii=False)}")
            print(f"[DEBUG] 模拟响应: 200 OK")
        except Exception as e:
            print(f"[DEBUG] 心跳模拟失败: {e}")
        await asyncio.sleep(interval)

# 使用 asyncio.to_thread 避免阻塞事件循环
        #     response = await asyncio.to_thread(
        #         requests.post, 
        #      HEARTBEAT_URL, 
        #        json=heartbeat_data, 
        #       timeout=5
        #    )